package org.kk.sentinel.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.kk.model.result.GlobalResult;
import org.kk.sentinel.handler.CustomBlockHandler;
import org.kk.sentinel.handler.CustomFallbackHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * the type of springcloud-alibaba
 *
 * <p>
 * .
 *
 * @author 铁甲小宝
 * @date 2023-01-31
 */
@RestController
public class SentinelController {

    private static int i = 0;
    @GetMapping("/testA")
    public String testA(){
        System.out.println(i++);
        return "Hello testA.";
    }
    @GetMapping("/testB")
    @SentinelResource(value = "testBB",
            fallbackClass = CustomFallbackHandler.class,
            fallback = "fallback",
            blockHandlerClass = CustomBlockHandler.class,
            blockHandler = "handlerException",
            exceptionsToIgnore = {IllegalArgumentException.class}
    )
    public GlobalResult testB(int p1){
        System.out.println(i++);
        if (p1==1){
            throw new NullPointerException();
        }
        if (p1==2){
            throw new IllegalArgumentException();
        }
        return new GlobalResult();
    }

    @GetMapping("/testC")
    @SentinelResource(value = "testCC", blockHandler = "deal")
    public String testHotKey(@RequestParam(value = "p1", required = false)String p1,
                             @RequestParam(value = "p2", required = false)String p2){
        System.out.println(i++);
        return "-----test hotkey..";
    }

    /**
     * testC发生异常的兜底方法， 即更改sentinel的默认提示。
     * @param p1
     * @param p2
     * @return
     */
    public String deal(String p1, String p2, BlockException exception){
        System.out.println("hava error");
        return "-----deal hotkey error ..";
    }
}
